<?php
/**
 * Created by PhpStorm.
 * User: prg
 * Date: 2017/3/3
 * Time: 21:30
 */

require_once 'db.php';
require_once 'response.php';

header('content-type:text/html;charset:utf-8');
header('Access-Control-Allow-Origin:*');
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');

$id = isset($_POST['reportId']) ? $_POST['reportId'] : '';

if(!is_numeric($id)) {
    return Response::show(401,'数据不合法');
}

try {
    $connect = Db::getInstance()->connect();
} catch (Exception $e) {
    return Response::show(403,'数据库连接失败',$e->getMessage());
}

//先通过查询语句确认id是有效的，然后执行删除语句，不然不好判断是否删除了
$sql = "SELECT check_status FROM report WHERE report_id={$id}";
$res = $connect->query($sql);

$data = array(
    'IsDelete' => false
);

if($res && $res->num_rows>0) {
    $res->free_result();

    //确认这个id是有效之后执行删除操作
    $sql = "DELETE FROM report WHERE report_id={$id}";
    $res = $connect->query($sql);
    if($res) {
        $data['IsDelete'] = true;
        return Response::show(200,'删除月报成功',$data);
    } else {
        return Response::show(402,'月报删除失败',$data);
    }

} else {
    return Response::show(404,'找不到月报数据',$data);
}

